package com.data.service;

import com.data.bean.Account;
import com.data.dbdao.RegisetdbDao;
import com.data.entity.User;
import com.data.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *
 */
@Service
public class RegistService {
    /**
     * 注册相关服务层
     */
    @Autowired
    private RegisetdbDao regisetdbDao;


    /**
     * 检测邮箱，账号，手机号,是否被占用的服务方法
     *
     * @param account
     * @return
     */
    public boolean checkEmailService(String account, String id) {

        boolean bool;
        //判断账号的类型，
        account = account.trim();
        String res = LoginMethodUtils.accountType(account);
        if (CheckDataUtils.stringUtils(id)) {
            id = id.trim();
            bool = regisetdbDao.checkEmailDao(res, account, id);
        } else {
            bool = regisetdbDao.checkEmailDao(res, account);
        }
        return bool;
    }

    /**
     * 提交注册信息，
     *
     * @param email
     * @param password
     * @return
     */
    public Account submitRegisterService(String username, String email, String password) {
        Account account = new Account();
        String id = DatabaseIdUtils.getDataId();
        account.setId(id);
        account.setUsername(username);
        account.setEmail(email);
        account.setPassword(Md5CodeUtils.md5(password));
        //创建账号的时间
        account.setCtime(new Date());
        //默认状态为未激活
        account.setState(FinalStringUtils.STATE_DEFAULT);
        //身份默认为0 ，即该身份尚未激活
        account.setIstate(FinalStringUtils.STATE_DEFAULT);
        //发动邮件
        //生成激活码
        String code = SendMailUtils.getActive(email);
        account.setCode(code);
        boolean bool = SendMailUtils.sendMail(email, code);
        //邮件是否发从成功
        if (bool) {
            bool = regisetdbDao.saveRegistDao(account);
            return bool ? account : null;
        }
        return null;
    }


    /**
     * 激活邮箱
     *
     * @param email
     * @param code
     * @return
     */
    public Boolean activeEmailService(String email, String code) {

        if (!CheckDataUtils.stringUtils(email, code)) {
            return false;
        }

        email = email.trim();
        code = code.trim();
        Account account = regisetdbDao.activeEmailDao(email, code);
        return account != null && regisetdbDao.changeAccountState(account.getId());
    }


    /**
     * 更换邮箱,就是更新实体信息
     *
     * @param user
     * @param id
     * @return
     */
    public Account changeEmailService(Account account, User user, String id) {
        if (!CheckDataUtils.stringUtilsID(id)) {
            return null;
        }

        account.setId(id);
        account.setUsername(user.getUsername());
        account.setPassword(Md5CodeUtils.md5(user.getPassword()));
        account.setEmail(user.getEmail());
        account.setCtime(new Date());
        String code = SendMailUtils.getActive(user.getEmail());
        account.setCode(code);
        boolean bool = regisetdbDao.changeEmailDao(account);
        if (bool) {
            //邮箱更换成功，发动邮件
            bool = SendMailUtils.sendMail(user.getEmail(), code);
            return bool ? account : null;
        }
        //邮箱更换失败
        return null;

    }


    /**
     * 发送手机验证码
     *
     * @param code
     * @return
     */
    public boolean sendPhoneCodeService(String code) {

        // 模拟发动验证
        return true;

    }

    public boolean reSendEmailService(String id, String email) {
        id = id.trim();
        email = email.trim();
        //生成激活码
        String code = SendMailUtils.getActive(email);
        boolean bool = regisetdbDao.changeEmailAndCodeDao(id, email, code, new Date());
        return bool && SendMailUtils.sendMail(email, code);
    }
}
